<?php
/*
 * classStudent控制器  
 * Tobey
 * */
require_once "baseController.class.php";
class classStudentController extends baseController{
	/**
     * 获得班级的学生基本信息
     * get    展示模版
     * ajax   获取信息  cla_id
     * */
	public function classStuInfo(){
		if(IS_GET) {
			//$this->display();
		}  else if(IS_AJAX && IS_LOCAL){
            session_start();
            $state = $_SESSION["isTeacher"];
            session_write_close();
            if($state == "true"){        //获取教师的班级学生情况
                $data = $this->classPower();
                if($data["status"] == 1){
                    if (isset($_POST["np"]) && is_numeric($_POST["np"])) {
                        $p = $_POST["np"];
                    } else {
                        $p = 1;
                    }
                    $sql = D();
                    if(IS_PC){
                        $num = 20;
                    } else{
                        $num = 10;
                    }
                //获取分页情况
                    $d["pageInfo"] = $sql->table(array("class_student"=>""))->where(array("class_cla_id"=>$_POST["cla_id"],"is_pass"=>1))->page($p,$num);
                    $d["student"] = $sql->table(array("class_student"=>"cs","student"=>"stu"))->where(array("cs.class_cla_id"=>$_POST["cla_id"],"is_pass"=>1,"__string__"=>"cs.student_stu_id = stu.stu_id"))->field(array("stu.stu_id","stu.stu_name","stu.stu_number","stu.stu_email","stu.sex"))->limit(($d["pageInfo"]["np"]-1)*$d["pageInfo"]["num"],$d["pageInfo"]["num"])->select();
                    $data = $this->message(1,"获取班级学生基本信息成功!",$d);
                    unset($sql,$p,$d);
                }
            } else{         //获取学生的班级同学情况
                $data = $this->stuClassPower();
                if($data["status"] == 1){
                    if (isset($_POST["np"]) && is_numeric($_POST["np"])) {
                        $p = $_POST["np"];
                    } else {
                        $p = 1;
                    }
                    $sql = D();
                    if(IS_PC){
                        $num = 20;
                    } else{
                        $num = 10;
                    }
                //获取分页情况
                    $d["pageInfo"] = $sql->table(array("class_student"=>""))->where(array("class_cla_id"=>$_POST["cla_id"],"is_pass"=>1))->page($p,$num);
                    $d["student"] = $sql->table(array("class_student"=>"cs","student"=>"stu"))->where(array("cs.class_cla_id"=>$_POST["cla_id"],"is_pass"=>1,"__string__"=>"cs.student_stu_id = stu.stu_id"))->field(array("stu.stu_id","stu.stu_name","stu.stu_number","stu.stu_email","stu.sex"))->limit(($d["pageInfo"]["np"]-1)*$d["pageInfo"]["num"],$d["pageInfo"]["num"])->select();
                    $data = $this->message(1,"获取班级学生基本信息成功!",$d);
                    unset($sql,$p,$d);
                }
            }
            $this->ajaxReturn($data);
            unset($data,$state);
        } 
    }

	/**
	 * 获取申请的学生的名单(基本信息)
	 * ajax    cla_id
	 */
	public function getApplyStu(){
		if(IS_AJAX && IS_LOCAL){
			$data = $this->classPower();
			if($data["status"] == 1){
				$data = $this->message(1,"获取申请的学生的名单成功!",D()->table(array("class_student"=>"cs","student"=>"stu"))->where(array("cs.class_cla_id"=>$_POST["cla_id"],"is_pass"=>0,"__string__"=>"cs.student_stu_id = stu.stu_id"))->field(array("stu.stu_id","stu.stu_name","stu.stu_number","stu.stu_email","stu.sex"))->select());
			}
		} else{
			$data = $this->message(0,"非法操作!!","");
		}
		$this->ajaxReturn($data);
		unset($data);
	}

	/**
	 * 批准申请学生加入到班级中
	 * ajax   cla_id     stu_id(可数组)
	 */
	public function allowJoin(){
		if(IS_AJAX && IS_LOCAL){
			$data = $this->classPower();
			if($data["status"] == 1){
				$sql = D();
            	$sql->startTrans();			//开启事物
            	$success = $sql->table(array("class_student"=>""))->where(array("class_cla_id"=>$_POST["cla_id"],"student_stu_id"=>array("in",$_POST["stu_id"],"is_pass"=>0)))->update(array("is_pass"=>1));
            	if($success && $sql->table(array("class"=>""))->where(array("cla_id"=>$_POST["cla_id"]))->field(array("cla_stu_num"))->setInc($success)){
            		$sql->commit();
            		$data = $this->message(1,"批准学生申请成功!","");
            	} else{
            		$sql->rollback();
            		$data = $this->message(0,"批准学生申请失败!","");
            	}
            }
        } else{
        	$data = $this->message(0,"非法操作!!","");
        }
        $this->ajaxReturn($data);
        unset($data);
    }

	/**
     * 从申请中移除学生的申请
     * ajax   cla_id     stu_id(可数组)
     * */
	public function removeApply(){
		if(IS_AJAX && IS_LOCAL){
			$data = $this->classPower();
			if($data["status"] == 1) {
				if(D()->table(array("class_student"=>""))->where(array("class_cla_id"=>$_POST["cla_id"],"student_stu_id"=>array("in",$_POST["stu_id"],"is_pass"=>0)))->delete()){
					$data = $this->message(1,"移除学生的申请成功!","");
				} else{
					$data = $this->message(0,"移除学生的申请失败!","");
				}
			}
		} else{
			$data = $this->message(0,"非法操作!!","");
		}
		$this->ajaxReturn($data);
		unset($data);
	}

	/**
     * 从班级中移除学生
     * ajax  cla_id  stu_id
     * */
	public function removeStu(){
		if(IS_AJAX && IS_LOCAL){
			$data = $this->classPower();
			if($data["status"] == 1) {
				$sql = D();
            	$sql->startTrans();			//开启事物
            	$success = $sql->table(array("class_student"=>""))->where(array("class_cla_id"=>$_POST["cla_id"],"student_stu_id"=>array("in",$_POST["stu_id"]),"is_pass"=>1))->delete();
            	if($success && $sql->table(array("class"=>""))->where(array("cla_id"=>$_POST["cla_id"]))->field(array("cla_stu_num"))->setDec($success)){
                    $sql->commit();
                    $data = $this->message(1,"移除学生成功!",$success);
                } else{
                    $sql->rollback();
                    $data = $this->message(0,"移除学生失败!","");
                }
            }
        } else{
        	$data = $this->message(0,"非法操作!!","");
        }
        $this->ajaxReturn($data);
        unset($data);
    }

    /**
     * 根据班级查询到该班级的学生  导入
     * ajax  cla_id    
     * */
    public function getClassStu(){
    	if(IS_AJAX && IS_LOCAL){
    		$data = $this->classPower();
    		if($data["status"] == 1) {
    			$data = $this->message(1,"获取班级学生基本信息成功!",D()->table(array("class_student"=>"cs","student"=>"stu"))->where(array("class_cla_id"=>$_POST["cla_id"],"is_pass"=>1,"__string__"=>"cs.student_stu_id = stu.stu_id"))->field(array("stu.stu_id","stu.stu_number","stu.stu_name","stu.stu_email","stu.sex"))->select());
    		}
    	} else{
    		$data = $this->message(0,"非法操作!!","");
    	}
    	$this->ajaxReturn($data);
    	unset($data);
    }

    /**
     * 导入学生功能(单个多个均可)
     * ajax
     * */
    public function import(){
    	if(IS_AJAX && IS_LOCAL){
    		$data = $this->classPower();
    		if($data["status"] == 1) {
                $count = 0;         //计算成功加入的个数
                $sql = D();
                for($i=0;$i<count($_POST["stu_id"]);$i++){
                	$where["class_cla_id"] = $_POST["cla_id"];
                	$where["student_stu_id"] = $_POST["stu_id"][$i];
                    $check = $sql->table(array("class_student"=>""))->where($where)->field(array("is_pass"))->find();   //查看本班级下有没有!
                    if($check){           //有的情况下,如果是申请的话就通过!
                    	if($check["is_pass"] == 0) {
                    		$sql->table(array("class_student"=>""))->where($where)->update(array("is_pass"=>1));
                    		$count++;
                    	} else{
                            continue;      //跳过本次循环
                        }
                    } else{                 //没有的情况下就要导入
                    	$add = $where;
                    	$add["is_pass"] = 1;
                    	if($sql->table(array("class_student"=>""))->add($add)){
                    		$count++;
                    	}
                    	unset($add);
                    }
                    unset($where,$check);
                }
                if($count > 0){            //有成功的是1
                	$sql->table(array("class"=>""))->where(array("cla_id"=>$_POST["cla_id"]))->field(array("cla_stu_num"))->setInc($count);
                	$data = $this->Message(1,"导入学生成功!",array("success"=>$count,"error"=>count($_POST["stu_id"]) - $count));
                } else{                   //1个都不成功就0
                	$data = $this->Message(0,"导入学生失败!","");
                }
                unset($count,$sql,$i);
            }
            $this->ajaxReturn($data);
            unset($data,$count);
        }
    }

    /*
    学生选课   登陆学生申请加入某些班级(可以是多个班级  cla_id是数组)  暂时加入的学生都是未通过is_filed=0
    ajax  本地  cla_id(数组)
    */
    public function stuJoin(){
        if(IS_AJAX && IS_LOCAL && isset($_POST["cla_id"])){
            $cla_id = $_POST["cla_id"];
            $cla_id_len = count($cla_id);
            $sql = D(); //开启数据库连接
            $table = array("class_student"=>"");
            session_start();    
            $add["student_stu_id"] = $_SESSION["id"];       //登陆者的id就是学生id
            session_write_close();
            $add["is_pass"] = 0;        //默认不通过(暂时)
            $success = 0;       //计数
            for($i = 0;$i<$cla_id_len;$i++){
                $add["class_cla_id"] = $cla_id[$i];
                if($sql->table($table)->where(array("class_cla_id"=>$cla_id[$i],"student_stu_id"=>$add["student_stu_id"]))->count() == 0 && is_numeric($sql->table($table)->add($add))){
                    $success++;
                }
            }
            if($success){
                $data = $this->message(1,"申请加入班级成功!","");
            } else{
                $data = $this->message(0,"申请加入班级失败!","");
            }
            $this->ajaxReturn($data);
            unset($data,$sql,$cla_id,$cla_id_len,$add,$table,$success);
        }
    }

    /*
    学生退选班级
    ajax 本地  cla_id
    */
    public function stuOut(){
        if(IS_AJAX && IS_LOCAL && isset($_POST["cla_id"])){
            $data = $this->stuClassPower();
            if($data["status"] == 1){
                session_start();
                $stu_id = $_SESSION["id"];      //学生id
                session_write_close();
                $sql = D();
                $sql->startTrans();     //开启事务管理
                if($sql->table(array("class_student"=>""))->where(array("class_cla_id"=>$_POST["cla_id"],"student_stu_id"=>$stu_id))->delete() && $sql->table(array("class"=>""))->where(array("cla_id"=>$_POST["cla_id"]))->field(array("cla_stu_num"))->setDec(1)){
                    $sql->commit();
                    $data = $this->message(1,"退选班级成功!","");
                } else{
                    $sql->rollback();
                    $data = $this->message(0,"退选班级失败!","");
                }
                unset($stu_id);
            }
            $this->ajaxReturn($data);
            unset($data);
        }
    }


	/**
  	 * 析构函数不用写任何内容也是可以的
  	 */
	public function __destruct(){}
}
